package ru.amse.tsyganov.jumleditor.commands;

import ru.amse.tsyganov.jumleditor.model.State;
import ru.amse.tsyganov.jumleditor.view.StateView;

/*package*/ class MoveStateCommand extends Command {
	
	private final StateView<? extends State> movingState;
	
	private final int oldX;
	
	private final int oldY;
	
	private final int newX;
	
	private final int newY;
	
	public MoveStateCommand(StateView<? extends State> movingState, 
			int moveToX, int moveToY, boolean checkModifications) {
		
		if (movingState == null) {
			throw new IllegalArgumentException();
		}
		
		this.movingState = movingState;
		
		oldX = movingState.getX();
		oldY = movingState.getY();
		
		newX = moveToX;
		newY = moveToY;
		
		if (checkModifications) {
			if (newX == oldX && newY == oldY) {
				throw new IllegalArgumentException();
			}
		}
	}
	
	@Override
	public void execute() {
		movingState.setX(newX);
		movingState.setY(newY);
	}

	@Override
	public void unexecute() {
		movingState.setX(oldX);
		movingState.setY(oldY);
	}

	protected StateView<? extends State> getMovingState() {
		return movingState;
	}

}
